package cn.neusoft.controller;


import cn.neusoft.pojo.User;
import cn.neusoft.query.UserQuery;
import cn.neusoft.service.IUserService;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    IUserService userService;

    @PostMapping
    public ResultBean add(@RequestBody User user){
        System.out.println("UserController--------add");
        System.out.println(user);

            return userService.add(user);
    }

    @PutMapping("/{id}")
    public ResultBean update(@RequestBody User user, @PathVariable("id") Integer id){
        System.out.println("UserController--------update");
        System.out.println(id);
        ResultBean resultBean = userService.getById(id);
        user.setUserId(id);
        return userService.update(user);
    }

    @DeleteMapping("/{id}")
    public ResultBean delete(@PathVariable("id") Integer id){
        System.out.println("UserController--------delete");
        System.out.println(id);
        return userService.delete(id);
    }

    @GetMapping("/{id}")
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("UserController--------getById");
        System.out.println(id);
        return userService.getById(id);
    }

    @GetMapping("/all")
    public ResultBean getAll(){
        System.out.println("UserController--------getAll");
        UserQuery userQuery = new UserQuery();
        return userService.getAll(userQuery);
    }

}
